package gl.common;

import gl.App;
import gl.uitls.Lg;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

public class BaseControl {

	public static void get(String uri, AjaxCallBack<String> callback,
			Object... args) {
		try {
			Lg.i("http", "" +String.format(uri, args));
			App.mFinalhttp.get(String.format(uri, args), callback);
		} catch (Exception e) {
			e.printStackTrace();
			callback.onFailure(e, 0, "");
		}

	}

	public static void getWithToken(String uri, AjaxCallBack<String> callback,
			Object... args) {
		String url;
		if (args == null || args.length == 0) {
			url = uri + "token=" + UserManger.getInstance().token;
		} else {
			url = String.format(uri, args) + "&token="
					+ UserManger.getInstance().token;
		}
		Lg.i("http", "" + url);
		if (!UserManger.getInstance().checkToken()) {
			callback.onFailure(null, 401, "token is null");
			return;
		}

		App.mFinalhttp.get(url, callback);
	}

	public static void getWithTokenNoProgress(String uri,
			AjaxCallBack<String> callback, Object... args) {
		String url;
		if (args == null || args.length == 0) {
			url = uri + "token=" + UserManger.getInstance().token;
		} else {
			url = String.format(uri, args) + "&token="
					+ UserManger.getInstance().token;
		}
		Lg.i("http", "" + url);
		if (!UserManger.getInstance().checkToken()) {
			callback.onFailure(null, 401, "token is null");
			return;
		}

		App.mFinalhttp.get(url, callback);
	}

	public static void postWithToken(String uri, AjaxCallBack<String> callback,
			AjaxParams ap) {
		String url;

		url = uri + "?token=" + UserManger.getInstance().token;
		Lg.i("http", "http post>>" + url);
		if (!UserManger.getInstance().checkToken()) {
			callback.onFailure(null, 401, "token is null");
			return;
		}

		App.mFinalhttp.post(url, ap, callback);
	}
}
